module one {
	export class SoundCtr {
		public static TOTAL:number = 13;

		private _volume : number = 1;
		public get volume() : number {
			return this._volume;
		}
		public set volume(v : number) {
			this._volume = v;
		}

		public constructor() {
		}

		public playSounds(names:string[]):void {
			if (this._volume == 0) {
				return;
			}
			if (this.isClosed) {
				return;
			}
			let self = this;

			if (this.onceSounds.length >= SoundCtr.TOTAL) {
				let ch:egret.SoundChannel = this.onceSounds.shift();
				ch.stop();
			}
			let p = ()=> {
				if (this._volume == 0) {
					return;
				}
				let name = names.shift();
				let sound = getSound(name);
				if (sound == null) {
					if (names.length > 0) {
						p();
					}
					return;
				}

				let channel:egret.SoundChannel = sound.play(0, 1);
					channel.volume = this.volume;
					this.onceSounds.push(channel);
					channel.once(egret.Event.SOUND_COMPLETE, function f(){
						let index = self.onceSounds.indexOf(channel);
						if (index >= 0) {
							self.onceSounds.splice(index, 1);
						}

						if (names.length > 0) {
							p();
						}
					}, channel);
			}

			p();
		}

		private onceSounds:egret.SoundChannel[] = [];

		private sounds:{[count:number]:egret.SoundChannel} = {};
		private static SOUND_COUNT:number = 0;

		public stop(count:number):void {
			let channel = this.sounds[count];
			if (channel) {
				channel.stop();

				let index = this.onceSounds.indexOf(channel);
				if (index >= 0) {
					this.onceSounds.splice(index, 1);
				}

				delete this.sounds[count];
			}
		}
		
		private playCnt:number = 0;
		public play(name:string, startTime:number, playTimes:number):number {
			name = getSoundKey(name);

			if (this._volume == 0) {
				return -1;
			}
			if (this.isClosed) {
				return -1;
			}
			if (this.onceSounds.length >= SoundCtr.TOTAL) {
				let ch:egret.SoundChannel = this.onceSounds.shift();
				ch.stop();
			}
			
			let self = this;
			this.playCnt++;

			let sound = RES.getRes(name)
			if (sound == null) {
				let key = this.playCnt;
				RES.getResAsync(name, () => {
					if (this.playCnt == key) {
						this.play(name, startTime, playTimes);
					}
				}, this);
				return;
			}
			
			let channel:egret.SoundChannel = sound.play(startTime, playTimes);
			channel.volume = this._volume;
				// console.log("SoundCtr " + 1111)
			channel.once(egret.Event.SOUND_COMPLETE, function f(){
				// console.log("SoundCtr " + 1111)
				let index = self.onceSounds.indexOf(channel);
				if (index >= 0) {
					self.onceSounds.splice(index, 1);
				}

				for (let count in self.sounds) {
					if (self.sounds[count] == channel) {
						delete self.sounds[count];
						break;
					}
				}
			}, channel);

			this.onceSounds.push(channel);

			SoundCtr.SOUND_COUNT++;
			this.sounds[SoundCtr.SOUND_COUNT] = channel;

			return SoundCtr.SOUND_COUNT;
		}

		private isClosed:boolean = false;
		public close():void {
			this.isClosed = true;
			while (this.onceSounds.length) {
				let channel = this.onceSounds.shift();
				channel.volume = 1;
				channel.stop();
			}
			this.onceSounds.length = 0;

			this.sounds = {};
		}

		public open():void {
			this.isClosed = false;
		}

		public stopAll():void {
			while (this.onceSounds.length) {
				let channel = this.onceSounds.shift();
				channel.volume = 1;
				channel.stop();
			}
			this.onceSounds.length = 0;
			this.sounds = {};
		}
	}
}